" _____           __     ___
"|__  /______  __ \ \   / (_)_ __ ___
"  / /|_  /\ \/ /  \ \ / /| | '_ ` _ \
" / /_ / /  >  <    \ V / | | | | | | |
"/____/___|/_/\_\    \_/  |_|_| |_| |_|


let mapleader=" "
let g:KEYBORD_TYPE = "qwer"
" let g:KEYBORD_TYPE = "colemak"

function! MySys()
    if has("win32")
        return "windows"
    else
        return "linux"
    endif
endfunction

let g:plug_file_path = ""
let g:DOCUMENT_PATH = "D:\\Documents"
let g:DOWNLOAD_PATH = 'D:\\Downloads'
let g:PICTURE_PATH = 'D:\\Pictures'
let g:VIDEO_PATH = 'D:\\Videos'
let g:DESK_PATH = 'C:\\Users\\zzx\\Desktop'
let g:PG_PATH = g:DOCUMENT_PATH.'\\Programmings'

if has("win32")
    let g:plug_file_path = "~/AppData/Local/nvim/plugged"
    let g:VIMPATH = "~/AppData/Local/nvim"
else
    let g:plug_file_path = "~/.config/nvim/plugged"
    let g:VIMPATH = "~/.config/nvim"
endif

" set foldlevel=99
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1,gbk,gb2312
set shortmess+=c
set updatetime=100
set termencoding=utf-8
set fileencoding=utf-8
set timeoutlen=1000
set ttimeoutlen=10
set shell=cmd.exe
" set shell=powershell.exe

filetype on
filetype indent on
filetype plugin on
filetype plugin indent on

set noswapfile
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set scrolloff=99
set sidescrolloff=6
set tw=0
set indentexpr=
set backspace=indent,eol,start
"set foldmethod=indent
"set foldlevel=99
set foldmethod=marker
set laststatus=2
set autochdir
set shortmess=atl
set formatoptions=tcrqn
set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr:hor20,o:hor50
set signcolumn=yes
" let g:gitgutter_sign_allow_clobber = 1
" highlight GitGutterAdd    guifg=#009900 ctermfg=2
" highlight GitGutterChange guifg=#bbbb00 ctermfg=3
" highlight GitGutterDelete guifg=#ff2222 ctermfg=1
" \,a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor
" \,sm:block-blinkwait175-blinkoff150-blinkon175

"let &t_SI = \"<Esc>]50;CursorShape=1\x7"
"let &t_SR = \"<Esc>]50;CursorShape=2\x7"
"let &t_EI = \"<Esc>]50;CursorShape=0\x7"
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

let &t_ut=''
set nocompatible
set number
" set relativenumber
syntax on
set wrap
set linebreak
set showcmd
set noshowmode
set wildmenu
set hlsearch
let loaded_matchparen = 0
set incsearch
set ignorecase
set smartcase
set clipboard+=unnamed,unnamedplus
set mouse=
set cursorline
"set cursorcolumn
set cmdheight=1
set noerrorbells
set showmatch
"set visualbell
set autoindent
set smartindent
set cindent
set pyxversion=3
" set pythonthreedll=python39.dll
"set fillchars=vert:\,stl:\,stlnc:\
set hidden
" set tags=tags
" set tags+=./tags

" exec 'nohl'

" if has("nvim")
" autocmd TermOpen * startinsert
" endif

" source ~/.config/nvim/map.vim
" source ~/.config/nvim/plug.vim

autocmd WinLeave * setlocal nocursorline
" autocmd WinEnter * setlocal cursorline
autocmd BufEnter * setlocal cursorline
autocmd BufEnter,BufRead,BufNewFile *.md set filetype=markdown
autocmd BufEnter,BufRead,BufNewFile *.md set fdm=marker
autocmd BufEnter,BufRead,BufNewFile vifmrc set filetype=vim
autocmd BufWritePre * :%s/\s\+$//e
autocmd FileType vim setlocal commentstring=\"\ %s
autocmd FileType c,cpp,json setlocal commentstring=//\ %s
autocmd FileType py setlocal commentstring=\#\ %s
" autocmd FileType markdown setlocal commentstring=<!--\ %s\-->
autocmd FileType markdown setlocal commentstring=[COMMENT]:\ #\ \"%s\"
autocmd FileType autohotkey setlocal commentstring=\;\ %s
autocmd FileType markdown set nocindent
autocmd FileType autohotkey setl omnifunc=ahkcomplete#Complete

" U<C-j> for both expand and jump (make expand higher priority.)

if g:KEYBORD_TYPE == 'colemak'
    noremap <nowait> <LEADER>nn :call Compile()<CR>
    noremap <nowait> <LEADER>ne :call CompileAndRun()<CR>
    noremap <nowait> <LEADER>hh :call Debug()<CR>
    noremap <LEADER>ee :call ZzxRun()<CR>
    noremap <LEADER>en :call ZzxRedirectRun()<CR>
    nmap <silent> <nowait> <M-k> gcc
    vmap <silent> <nowait> <M-k> gc
elseif g:KEYBORD_TYPE == 'qwer'
    noremap <nowait> <LEADER>jj :call Compile()<CR>
    noremap <nowait> <LEADER>jk :call CompileAndRun()<CR>
    noremap <nowait> <LEADER>hh :call Debug()<CR>
    noremap <LEADER>kk :call ZzxRun()<CR>
    noremap <LEADER>kj :call ZzxRedirectRun()<CR>
    nmap <silent> <nowait> <M-n> gcc
    vmap <silent> <nowait> <M-n> gc
endif

" let curdir = substitute(getcwd(), $HOME, "~", "g")
let g:file_whole_name = @%
let file_exe_name = expand('%<').'.exe'
" let g:file_exe_name = expand('%<')
let g:file_name = expand('%:t<')

func! Debug()
    let name=g:file_whole_name
    if &filetype == 'cpp'
        exec 'tabe'
        exec 'term gdb.exe '.g:PG_PATH.'\\Cpp\\ctmp.exe'
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term gdb.exe '.g:PG_PATH.'\\Cpp\\ctmp.exe'
    elseif &filetype == 'go'
        exec 'tabe'
        exec 'term go build'
        "" call system('st -e sh -c \"g++ ' .g:file_name. ' -o ~/Documents/cpptmp.exe; read a"')
    endif
endfunc
func! Compile()
    let name=g:file_whole_name
    if &filetype == 'cpp'
        exec 'tabe'
        exec 'term g++ -g '.name.' -o '.g:PG_PATH.'\\Cpp\\ctmp.exe & echo. & echo Compile Done!'
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term gcc -g '.name.' -o '.g:PG_PATH.'\\Cpp\\ctmp.exe & echo. & echo Compile Done!'
    elseif &filetype == 'go'
        exec 'tabe'
        exec 'term go build'
    elseif &filetype == 'java'
        exec 'tabe %'
        exec 'term javac %'
        "" call system('st -e sh -c \"g++ ' .g:file_name. ' -o ~/Documents/cpptmp.exe; read a"')
    endif
endfunc

func! CompileAndRun()
    silent exec 'w!'
    let name=g:file_whole_name
    if &filetype == 'cpp'
        exec 'tabe'
        " 对cmd来说，&&是成功后再执行
        exec 'term chcp 65001 1>nul && g++ '.name.' -o '.g:PG_PATH.'\\Cpp\\ctmp.exe && '.g:PG_PATH.'\\Cpp\\ctmp.exe'
    elseif &filetype == 'html'
        " silent! exec "!start % &"
        exec "silent !start MicrosoftEdge.exe ".$PWD."/%"
    elseif &filetype == 'java'
        let className = split(name, '\.')[0]
        exec 'tabe'
        exec "term javac ".name." && java ".className
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term chcp 65001 1>nul && gcc '.name.' -o '.g:PG_PATH.'\\Cpp\\ctmp.exe && '.g:PG_PATH.'\\Cpp\\ctmp.exe'
    elseif &filetype == 'python'
        :tabe %
        :term chcp 65001 1>nul && python %
    elseif &filetype == 'go'
        exec 'tabe %'
        exec 'term chcp 65001 1>nul && go run %'
    elseif &filetype == 'markdown'
        exec "silent !start ".$PWD."/%"
    elseif &filetype == 'javascript'
        exec 'tabe %'
        exec 'term node %'
    elseif &filetype == 'typescript'
        exec 'tabe %'
        " exec 'term node %'
        exec 'term chcp 65001 1>nul && tsc && node.exe %<.js'
        " exec 'term chcp 65001 1>nul && tsc.cmd % --outFile '.g:PG_PATH.'\\Typescript\\tmp.js && node.exe tmp.js'
    elseif &filetype == 'dosbatch'
        " call system('start cmd.exe /c "D:\\Documents\\cpptmp.exe & echo. & echo. &  echo [process end] & pause"')
        exec 'tabe %'
        exec 'term %'
    elseif &filetype == 'cpp'
        " call system('start cmd.exe /c "D:\\Documents\\cpptmp.exe & echo. & echo. &  echo [process end] & pause"')
        exec 'tabe'
        exec 'term chcp 65001 1>nul && '.g:PG_PATH.'\\Cpp\\ctmp.exe'
        " exec 'startinsert'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term chcp 65001 1>nul && '.g:PG_PATH.'\\Cpp\\ctmp.exe'
        " call system('st -e sh -c "D:\\Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
        exec 'tabe %'
        exec "!javac %"
        exec "!time java %<"
    elseif &filetype == 'asm'
        exec "!lc3as %<"
    elseif &filetype == 'sh'
        :!time bash %
    elseif &filetype == 'python'
        :tabe %
        :term python %
    elseif &filetype == 'html'
        silent! exec "!".g:mkdp_browser." % &"
    elseif &filetype == 'markdown'
        " exec "InstantMarkdownPreview"
        exec "silent !start MicrosoftEdge.exe ".$PWD."/%"
    elseif &filetype == 'tex'
        silent! exec "VimtexStop"
        silent! exec "VimtexCompile"
    elseif &filetype == 'dart'
        exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
        silent! exec "CocCommand flutter.dev.openDevLog"
    elseif &filetype == 'javascript'
        set splitbelow
        :sp
        :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
    elseif &filetype == 'go'
        " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
        :tabe %
        :term chcp 65001 1>nul && go run %
    endif
endfunc

function! ZzxRedirectRun()
    if &filetype == 'cpp'
        " call system('start cmd.exe /c "D:\\Documents\\cpptmp.exe & echo. & echo. &  echo [process end] & pause"')
        exec 'tabe'
        exec 'term type input.txt | D:\\Documents\\Programmings\\Cpp\\ctmp.exe'
        " exec 'startinsert'
        " call system('st -e sh -c \"~/Documents/cpptmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'c'
        exec 'tabe'
        exec 'term type input.txt | D:\\Documents\\Programmings\\Cpp\\ctmp.exe'
        " call system('st -e sh -c "D:\\Documents/ctmp.exe; echo; echo; echo [process end]; read a"')
    elseif &filetype == 'java'
        exec "tabe %"
        " exec 'javac %'
        exec 'term javac % && type input.txt | java %<'
        " exec 'term type input.txt | java %<'
    elseif &filetype == 'asm'
        exec "!lc3as %<"
    elseif &filetype == 'sh'
        :!time bash %
    elseif &filetype == 'python'
        set splitbelow
        :sp
        :term python %
    elseif &filetype == 'html'
        silent! exec "!".g:mkdp_browser." % &"
    elseif &filetype == 'markdown'
        " exec "InstantMarkdownPreview"
        exec "silent !start chrome.exe ".$PWD."/%"
    elseif &filetype == 'tex'
        silent! exec "VimtexStop"
        silent! exec "VimtexCompile"
    elseif &filetype == 'dart'
        exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
        silent! exec "CocCommand flutter.dev.openDevLog"
    elseif &filetype == 'javascript'
        set splitbelow
        :sp
        :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
    elseif &filetype == 'go'
        " call system('st -e sh -c \"go run '.@%.'; echo; echo [ Process exit ]; read a"')
        exec 'tabe %'
        exec 'term type input.txt | go.exe run %'
    endif
endfunc

exec 'highlight Cursor guibg=Red'

" ##### auto fcitx  ###########
" let g:input_toggle = 1
" function! Fcitx2en()
"    let s:input_status = system("fcitx5-remote")
"    if s:input_status == 2
"       let g:input_toggle = 1
"       let l:a = system("fcitx5-remote -c")
"    endif
" endfunction

" function! Fcitx2zh()
"    let s:input_status = system("fcitx5-remote")
"    if s:input_status != 2 && g:input_toggle == 1
"       let l:a = system("fcitx5-remote -o")
"       let g:input_toggle = 0
"    endif
" endfunction

" set ttimeoutlen=150
" 退出插入模式
" autocmd InsertLeave * call Fcitx2en()
" 进入插入模式
" autocmd InsertEnter * call Fcitx2zh()
" ##### auto fcitx end ######
"
"
exec 'source '.g:VIMPATH.'/map.vim'
exec 'source '.g:VIMPATH.'/plug.vim'
nnoremap K 10k
" exec 'luafile '.g:VIMPATH.'/test.lua'
" let g:coc_disable_startup_warning = 1
